<template>
  <div class="app-container">
    <!-- 表格-条件筛区域  -->
    <div class="filter-container">
      <el-button class="filter-item" type="primary" icon="el-icon-refresh" @click=" getList()">刷新列表</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-refresh" @click="changeExpandState()">展开|折叠</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-edit" @click="handleCreate(null)">新增章节</el-button>
    </div>
    <tree-table :data="tableList" :columns="columns"
                :expand-all="expandAll" :listLoading="listLoading" border  ref="organTree"
                rowKey="id"   >
      <el-table-column label="操作" width="270" align="center">
        <template slot-scope="scope">
          <el-button v-if="!scope.row.pid" type="primary" size="mini" @click="handleCreate(scope.row.id)">新增章节视频</el-button>
          <el-button type="published" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </tree-table>
    <!--新增编辑界面-->
    <el-dialog :title="operation?'新增':'编辑'" width="45%" top="5vh" :visible.sync="dialogFormVisible" :modal="false" :close-on-click-modal="false">
      <el-form :model="dataForm" label-width="140px" :rules="dataFormRules" ref="dataForm" style="width: 80%">
<!--        <el-form-item label="创建时间" prop="createTime">-->
<!--          <el-input v-model="dataForm.createTime" auto-complete="off" placeholder="请输入创建时间"/>-->
<!--        </el-form-item>-->
        <el-form-item label="章节名称" prop="groupName">
          <el-input v-model="dataForm.name" auto-complete="off" placeholder="请输入章节名称"/>
        </el-form-item>
        <el-form-item label="排序" prop="orderNo">
          <el-input v-model="dataForm.orderNo" auto-complete="off" placeholder="请输入排序"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click.native="closeDialog()">取消</el-button>
        <el-button size="mini" @click.native="saveFormData(0)" :loading="editLoading" type="primary">提交 </el-button>
      </div>
    </el-dialog>

    <!--新增编辑界面-->
    <el-dialog :title="operation?'新增':'编辑'" width="45%" top="5vh" :visible.sync="itemDialogFormVisible" :modal="false" :close-on-click-modal="false">
      <el-form :model="itemDataForm" label-width="140px" :rules="itemDataFormRules" ref="itemDataForm" style="width: 80%">
        <el-form-item label="课程类型">
          <el-select v-model="this.typeId" disabled >
            <el-option v-for="item in courseList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程名称" prop="itemName">
          <el-input v-model="itemDataForm.name" auto-complete="off" placeholder="请输入视频名称"/>
        </el-form-item>
        <el-form-item v-if="this.typeId !=='2' " label="课程视频" prop="videoId">
          <p v-if="itemDataForm.videoId">{{ videoName }}</p>
          <el-button size="mini" @click="vodUploadDialog=true" >上传</el-button>
<!--          <el-button size="mini" v-if="itemDataForm.videoId" @click="showVideo(itemDataForm.videoId)">预览</el-button>-->
<!--          <el-input v-model="itemDataForm.videoId" auto-complete="off" placeholder="请输入课程视频路径"/>-->
        </el-form-item>
        <el-form-item label="描述" prop="itemDesc">
          <el-input v-model="itemDataForm.itemDesc" auto-complete="off" placeholder="请输入排序"/>
        </el-form-item>
        <el-form-item label="排序" prop="orderNo">
          <el-input v-model="itemDataForm.orderNo" auto-complete="off" placeholder="请输入排序"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click.native="closeItemDialog()">取消</el-button>
        <el-button size="mini" @click.native="saveFormData(1)" :loading="editLoading" type="primary">提交 </el-button>
      </div>
    </el-dialog>

    <el-dialog title="点播上传" width="45%" top="5vh" :visible.sync="vodUploadDialog" :modal="false" :close-on-click-modal="false"
               ref="vodUpload">
      <UploadAuth v-if="vodUploadDialog" v-model="itemDataForm.videoId" @getVideoId="getVideoId" @showVideo="showVideo"></UploadAuth>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click.native="closeVodDialog()">确定</el-button>
<!--        <el-button size="mini" @click.native="" :loading="editLoading" type="primary">提交 </el-button>-->
      </div>
    </el-dialog>

<!--    <el-dialog-->
<!--        title="视频预览" width="45%" top="5vh" :visible.sync="showVodDialog" :modal="false" :close-on-click-modal="false"-->
<!--        ref="vodUpload"-->
<!--    >-->
<!--      <VodPlayer :source="playAuth" :autoplay="true" :vid="vid" ref="player" v-if="showVodDialog"></VodPlayer>-->
<!--    </el-dialog>-->
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 分页栏信息
import Upload from '@/components/CmUpload' //上传
import Item from './courseItem.vue'
import treeTable from '@/components/TreeTable'
import UploadAuth from '../vod/UploadAuth.vue'
import VodPlayer from "@/components/VodPlayer";

export default {
  name: 'group',
  components: {
    Pagination,
    Item,
    Upload,
    treeTable,
    UploadAuth,
    VodPlayer,
  },
  directives: {   },
  filters: { },
  props:{
    // 课程ID
    courseId:{
        type:String,
    },
    //直播课or 录播课 or 视频会议 or 面授课
    typeId:{
      type:String,
    },
    // 计划ID
    planId:{
        type:String,
    }
  },
  data() {
    return {
      tableKey: 0,
      tableList: [],//表格数据源
      tableTotal: 0,//表格总条数数据
      listLoading: true,
      expandAll: false, // 默认不展开所有节点
      vodUploadDialog: false, // 默认不展开所有节点
      pageRequest: { pageNum: 1, pageSize: 10,
        searchParam:{
          SEARCH_EQ_course_id:this.courseId
        }
      },
      courseList:[
          { value: '1', label: '录播课' },
          { value: '2', label: '直播课' },
          { value: '3', label: '面授课' },
          { value: '4', label: '视频会议' },
      ],
      searchParam:{},
      dialogFormVisible: false,//默认不显示弹出框
      itemDialogFormVisible: false,//默认不显示弹出框
      operation: true, // true:新增, false:编辑
      editLoading: false,//保存表单时loading框
      showVodDialog: false,//保存表单时loading框
      //vod 参数
      cover: '', //视频封面
      vid: '', //视频vid
      playAuth: '', //鉴权地址

      columns: [
        { text: '章节名',  value: 'name', align:'left' },
        { text: '推流域名' , value: 'pushUrl' , align: 'left'},
        { text: '拉流域名' , value: 'pullUrl' , align: 'left'},
      ],
      fileList: [],
      // 新增编辑界面数据
      dataForm: {},
      itemDataForm: {},
      //表单检验规则
      dataFormRules: {},
      itemDataFormRules: {},
      vodAction:'',
      videoUrl:'',
      videoName:'已上传',
    }
  },
  created() {
    this.getList();
  },
  methods: {

    // 改变展开收缩
    changeExpandState() {
      this.expandAll = !this.expandAll
      if (this.expandAll) {
        this.$refs.organTree.expandAllRow();
      } else {
        this.$refs.organTree.collapseAllRow();
      }
      this.$forceUpdate();
    },

    //获取表格数据
    getList() {
      this.listLoading = true
      //【API请求】----获取表格列表数据
      this.$api.getSCourseGroupList({courseId: this.courseId ,planId:this.planId?this.planId:''}).then(response => {
        this.tableList = response.data
        // this.tableTotal = response.data.total
        this.listLoading = false;
      }).catch((res) => {
        this.$message({message: '列表获取失败', type: 'error'})
      })
    },
    //表格数据筛选
    handleFilter() {
        this.pageRequest = {
            pageNum: 1,
            pageSize: 10,
        }
      this.pageRequest.searchParam = this.searchParam
      this.getList()
    },
    //【按钮点击】--- 弹出新增表单
    handleCreate(pid) {
      this.operation = true;
      this.initDataForm();
      if (pid) {
        this.itemDataForm.groupId = pid
        this.itemDialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['itemDataForm'].clearValidate()
          this.editLoading = false
        })
      } else {
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
          this.editLoading = false;
        })
      }
    },
    //【按钮点击】--- 弹出编辑表单
    handleUpdate(row) {
      this.operation = false;
      if (row.pid) {
        this.itemDataForm = Object.assign({}, row)
        //显示编辑菜单
        this.itemDialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['itemDataForm'].clearValidate()
          this.editLoading = false;
        })
      } else {
        this.dataForm = Object.assign({}, row)
        //显示编辑菜单
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
          this.editLoading = false;
        })
      }


    },
    //保存表单数据
    saveFormData(row) {
      if (row === 1) {
        this.$refs['itemDataForm'].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.editLoading = true;
          //【API请求】----保存数据
          this.itemDataForm = {
            durationTotal: this.itemDataForm.durationTotal,
            itemName: this.itemDataForm.name,
            videoId: this.itemDataForm.videoId,
            orderNo: this.itemDataForm.orderNo,
            groupId: this.itemDataForm.groupId,
            itemDesc: this.itemDataForm.itemDesc,
            id: this.itemDataForm.id,
            courseId: this.courseId,
            planId:this.planId,
          }
          this.$api.saveSCourseItem(this.itemDataForm).then(() => {
            this.editLoading = false;//关闭loading框
            this.itemDialogFormVisible = false;//关闭表单页面
            this.initDataForm();//重置表单数据
            this.getList();//重新获取表格数据
            this.$notify({title: '成功', message: '保存成功', type: 'success', duration: 2000})
          })
        })
      } else {
        this.$refs['dataForm'].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.editLoading = true;
          //【API请求】----保存数据
          this.dataForm = {
            courseId: this.courseId,
            groupName: this.dataForm.name,
            orderNo: this.dataForm.orderNo,
            id: this.dataForm.id,
            planId:this.planId,
          }
          this.$api.saveSCourseGroup(this.dataForm).then(() => {
            this.editLoading = false;//关闭loading框
            this.dialogFormVisible = false;//关闭表单页面
            this.initDataForm();//重置表单数据
            this.getList();//重新获取表格数据
            this.$notify({title: '成功', message: '保存成功', type: 'success', duration: 2000})
          })
        })
      }
    },
    //【按钮点击】---删除行
    handleDelete(row) {


      this.$confirm('确认删除该记录吗？', '提示', {}).then(() => {
        if (row.pid) {
          this.$api.deleteSCourseItem({ids: row.id}).then(() => {
            this.$message({message: '操作成功', type: 'success'})
              this.pageRequest.pageNum = 1
            this.getList();
          })
        } else {
          //【API请求】---- 删除数据
          this.$api.deleteSCourseGroup({ids: row.id}).then(() => {
            this.$message({message: '操作成功', type: 'success'})
            this.getList();
          })
        }

      }).catch(() => {
        this.$message({message: '操作失败', type: 'error'})
      });
    },
    //初始化表单数据，重新渲染
    initDataForm() {
      this.dataForm = {
        id: '',
        courseId: '',
        groupName: '',
        orderNo: '',
        planId:'',
      };
      this.itemDataForm = {
        durationTotal: '',
        planId:'',
        itemName: '',
        videoId: '',
        orderNo: '',
        cover: '',
        groupId: '',
        itemDesc: '',
        id: '',
      };
    },
    //关闭表单弹出框
    closeDialog() {
      this.$refs['dataForm'].resetFields();
      this.dialogFormVisible = false
    },
    closeItemDialog() {
      this.$refs['itemDataForm'].resetFields();
      this.itemDialogFormVisible = false
    },

    closeVodDialog() {
      this.vodUploadDialog = false
    },

    getVideoId(data) {
      console.log(data)
      this.itemDataForm.videoId = data

    },
    showVideo(data) {
      console.log(data, "data")
      this.videoName = data
    },

  }
}
</script>

<style>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
